package com.jeff.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeff.common.BaseInfo;
import com.jeff.common.mConst;
import com.jeff.mybatis.page.Page;
import com.jeff.po.Dept;
import com.jeff.service.DeptService;
import com.jeff.util.ResultMap;

@Controller
@RequestMapping("dept")
public class DeptController {
	@Autowired
	public DeptService deptService;
	
	@RequestMapping("page")
	@ResponseBody
	public ResultMap page(Dept dept) {
		ResultMap rm = new ResultMap();
		try {
			dept.setId(null);
			dept.setState(mConst.STATE_T);
			Page<Dept> list = deptService.findByPage(dept);
			rm.success().page(list);
		} catch (Exception e) {
			e.printStackTrace();
			rm.fail().info("fail");
		}
		return rm;
	}
	
	@RequestMapping("parentDeptPage")
	@ResponseBody
	public ResultMap parentDeptPage(Dept dept) {
		ResultMap rm = new ResultMap();
		try {
			dept.setId(null);
			dept.setParentId(null);
			dept.setState(mConst.STATE_T);
			Page<Dept> list = deptService.findparentDeptByPage(dept);
			rm.success().page(list);
		} catch (Exception e) {
			e.printStackTrace();
			rm.fail().info("fail");
		}
		return rm;
	}
	
	@RequestMapping("secondDeptPage")
	@ResponseBody
	public ResultMap secondDeptPage(Dept dept,HttpServletRequest request) {
		ResultMap rm = new ResultMap();
		try {
			dept.setId(null);
			dept.setState(mConst.STATE_T);
			dept.setParentId(BaseInfo.GetUserDeptId(request));
			Page<Dept> list = deptService.findByPage(dept);
			rm.success().page(list);
		} catch (Exception e) {
			e.printStackTrace();
			rm.fail().info("fail");
		}
		return rm;
	}
	
	
	@RequestMapping("list")
	@ResponseBody
	public ResultMap getName(Boolean onlySecond,HttpServletRequest request) {
		ResultMap rm = new ResultMap();
		try {
			Dept dept = new Dept();
			dept.setId(null);
            dept.setState(mConst.STATE_T);
            if(onlySecond !=null&&onlySecond==true){
            	//用于管理员给自己的下属机构添加用户的机构列表
            	dept.setParentId(BaseInfo.GetUserDeptId(request));
            }
			List<Dept> list = deptService.list(dept);
			rm.success().data(list);
		} catch (Exception e) {
			e.printStackTrace();
			rm.fail().info("查询社团失败");
		}
		return rm;
		
	}
	
	
	
	@RequestMapping("update")
	@ResponseBody
	public ResultMap update(String id,String name) {
		ResultMap rm = new ResultMap();
		try {
			Dept dept = new Dept();
			dept.setId(id);
			dept.setName(name);
			deptService.update(dept);
			rm.success().info("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			rm.fail().info("修改失败");
		}
		return rm;
	}
	
	@RequestMapping("add")
	@ResponseBody
	public ResultMap add(Dept dept) {
		ResultMap rm = new ResultMap();
		try {
			dept.setState(mConst.STATE_T);
			deptService.add(dept);
			rm.success().info("增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			rm.fail().info("增加失败");
		}
		return rm;
		
	}
	@RequestMapping("addSecond")
	@ResponseBody
	public ResultMap addSecondDept(Dept dept,HttpServletRequest request) {
		ResultMap rm = new ResultMap();
		try {
			dept.setState(mConst.STATE_T);
			dept.setParentId(BaseInfo.GetUserDeptId(request));
			deptService.add(dept);
			rm.success().info("增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			rm.fail().info("增加失败");
		}
		return rm;
		
	}
	
	@RequestMapping("remove")
	@ResponseBody
	public ResultMap remove(Dept dept) {
		ResultMap rm = new ResultMap();
		try {
			String deptId = dept.getId();
			Dept myDept = deptService.getById(deptId);
			if(myDept.getParentId()==null||"".equals(myDept.getParentId())){
				Dept myDept1 = new Dept();
				myDept1.setId(null);
				myDept1.setParentId(dept.getId());
				myDept1.setState(mConst.STATE_T);
				List<Dept> dList = deptService.list(myDept1);
				if(dList!=null&&dList.size()!=0){
					return rm.fail().info("不能删除有子部门的社团");
				}
			}
			dept.setState(mConst.STATE_F);
			deptService.update(dept);
			rm.success().info("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			rm.fail().info("删除失败");
		}
		return rm;
		
	}
	
//	@RequestMapping("check")
//	@ResponseBody
//	public Boolean check(Dept dept) {
//		try {
//			Dept dp = deptService.check(dept);
//			if(dp!=null) {
//				return false;
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			return true;
//		}
//		return true;
//		
//	}
}
